/**
|-|     a 
 -     f  b
|-|.    g
       e  c
        d   .
命令：n0,n1,n2...n9分别显示数字0~9
命令：r 随便显示0~9

*/    
const PROGMEM int ledA = 2;
const PROGMEM int ledB = 3;
const PROGMEM int ledC = 4;
const PROGMEM int ledD = 5;
const PROGMEM int ledE = 6;
const PROGMEM int ledF = 7;
const PROGMEM int ledG = 8;
const PROGMEM int ledP = 9; // 点
const PROGMEM int btn = 11; // 按键
char cmd;
int num;

void setup() {
  for(int i=2; i <= 9; ++i)
  {
    pinMode(i, OUTPUT);
  }
  pinMode(btn, INPUT_PULLUP);
  Serial.begin(9600);
  cmd = 'r'; // 默认显示随机数
}

void loop() {
  bool btnFlag = digitalRead(btn);
  Serial.println(btnFlag); // 按键电压
  if(Serial.available())
  {
    cmd = Serial.read(); // 读取命令
    num = Serial.parseInt();
    Serial.read(); // 回车符
  }
  if(!btnFlag) // 按下
  {
    num = num + 1;
    cmd = 'x';
    if(num > 9)
    {
      num = 0;
    }
    writeNum(num);
    delay(500);
    return;
  }
  if(cmd)
  {
    switch(cmd)
    {
      case 'n':
        Serial.println(num);
        writeNum(num); // 显示输入数字
        break;
      case 'r':
        randomSeed(analogRead(A0)); // 随机种子
        num = random(0,10);
        writeNum(num); // 显示输入数字
        delay(100);
        break;
    }
  }
  delay(100);
}
/**
|-|     a 
 -     f  b
|-|.    g
       e  c
        d   .
*/  
void writeNum(int num)
{
    clearNum(); // 清空数字
    switch(num)
    {
      case 0:
        digitalWrite(ledA, HIGH);
        digitalWrite(ledB, HIGH);
        digitalWrite(ledC, HIGH);
        digitalWrite(ledD, HIGH);
        digitalWrite(ledE, HIGH);
        digitalWrite(ledF, HIGH);
        break;
      case 1:
        digitalWrite(ledE, HIGH);
        digitalWrite(ledF, HIGH);
        break;
      case 2:
        digitalWrite(ledA, HIGH);
        digitalWrite(ledB, HIGH);
        digitalWrite(ledG, HIGH);
        digitalWrite(ledD, HIGH);
        digitalWrite(ledE, HIGH);
        break;
      case 3:
        digitalWrite(ledA, HIGH);
        digitalWrite(ledB, HIGH);
        digitalWrite(ledC, HIGH);
        digitalWrite(ledD, HIGH);
        digitalWrite(ledG, HIGH);
        break;
      case 4:
        digitalWrite(ledF, HIGH);
        digitalWrite(ledB, HIGH);
        digitalWrite(ledC, HIGH);
        digitalWrite(ledG, HIGH);
        break;
      case 5:
        digitalWrite(ledA, HIGH);
        digitalWrite(ledF, HIGH);
        digitalWrite(ledG, HIGH);
        digitalWrite(ledC, HIGH);
        digitalWrite(ledD, HIGH);
        break;
      case 6:
        digitalWrite(ledA, HIGH);
        digitalWrite(ledG, HIGH);
        digitalWrite(ledC, HIGH);
        digitalWrite(ledD, HIGH);
        digitalWrite(ledE, HIGH);
        digitalWrite(ledF, HIGH);
        break;
      case 7:
        digitalWrite(ledA, HIGH);
        digitalWrite(ledB, HIGH);
        digitalWrite(ledC, HIGH);
        break;
      case 8:
        digitalWrite(ledA, HIGH);
        digitalWrite(ledB, HIGH);
        digitalWrite(ledC, HIGH);
        digitalWrite(ledD, HIGH);
        digitalWrite(ledE, HIGH);
        digitalWrite(ledF, HIGH);
        digitalWrite(ledG, HIGH);
        break;
      case 9:
        digitalWrite(ledA, HIGH);
        digitalWrite(ledB, HIGH);
        digitalWrite(ledC, HIGH);
        digitalWrite(ledD, HIGH);
        digitalWrite(ledF, HIGH);
        digitalWrite(ledG, HIGH);
        break;
    }
}

void clearNum()
{
  for(int i=2; i <= 9; ++i)
  {
    digitalWrite(i, LOW);
  }
}
